package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.BookFixBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.UnitAppointmentReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;
import com.winz.cultureapp.widge.TimerSelector;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 预约讲解
 */
public class BookExplainActivity extends BaseAppCompatActivity implements TimerSelector.OkClickListener {

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.et_name)
    EditText mEt_name;
    @BindView(R.id.et_company)
    EditText mEt_company;
    @BindView(R.id.et_num)
    EditText mEt_num;
    @BindView(R.id.et_phone)
    EditText mEt_tell;
    @BindView(R.id.tv_time)
    TextView mTv_time;

    private String item_id; // 展馆ID

    private TimerSelector ts_time;
    private String timeDate; // 当前选择的日期
    private long time;

    @Override
    protected int contentView() {
        return R.layout.activity_book_explain;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText(getString(R.string.book_explain_top_title));

        ts_time = new TimerSelector(this, "yyyy-MM-dd HH:mm:ss");
        ts_time.setOkClickListener(this);
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                item_id = intent.getExtras().getString(Constants.KEY.EXHIBITION.ITEM_ID);
            }
        }
    }

    @OnClick({R.id.fl_back, R.id.btn_submit,R.id.ll_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.btn_submit:
                if(isAllRight()){
                    submit();

                }
                break;
            case R.id.ll_time:
                ts_time.show(mTv_time, 0);
                break;
        }
    }

    private void submit() {
        showProgressDialog();

        HttpUitls.onPostByToken("unit_appointment",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                new UnitAppointmentReq(timeDate,getEditTextStr(mEt_company),getEditTextStr(mEt_tell),getEditTextStr(mEt_num),getEditTextStr(mEt_name),item_id),
                new OnResponeListener<BookFixBean>() {
            @Override
            public void onSuccess(BookFixBean bean) {
                Logger.d("unit_appointment==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    showMsg("预约成功");
                    Bundle bundle = new Bundle();
                    bundle.putInt("index",3);
                    startActivity(PersonalRecordActivity.class, true,bundle);
                } else {
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("unit_appointment==>>", error);
                dismissProgressDialog();
            }
        });
    }

    /**
     * 资料是否全部填完
     * @return
     */
    private boolean isAllRight() {
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入预约者姓名");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_company))){
            showMsg("请输入预约者单位");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_num))){
            showMsg("请输入预约人数");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_tell))){
            showMsg("请输入预约者联系方式");
            return false;
        }
        if (TextUtils.isEmpty(timeDate)){
            showMsg("请输入预约时间");
            return false;
        }
        return true;
    }

    @Override
    public void okClick(View view, String date, long time, int flag) {
        if (flag == 0) {
            mTv_time.setText(date);
            timeDate = date;
            time = time;
        }
    }
}
